﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CID.Tests.Support;
using NUnit.Framework;
using CID.Domain.Infrastructure;
using System.Xml;

namespace CID.Domain.Tests.Infrastructure.ParameterValue
{
    [TestFixture]
    public class ParameterValueSerializerTests : BaseFunctionalTests
    {
        #region Static constants and values
        private const string StringValue = "String for Testing";
        private const string SerializedStringValue = "String for Testing";

        private const int IntegerValue = 1234;
        private const string SerializedIntegerValue = "1234";

        private const decimal DecimalValue = 12.345m;
        private const string SerializedDecimalValue = "12.345";

        private static readonly DateTime DateTimeValue = new DateTime(2011, 10, 9, 18, 07, 06,543, DateTimeKind.Utc);
        private const String SerializedDateTimeValue = "2011-10-09T18:07:06.543Z";

        private const bool BooleanValue = true;
        private const string SerializedBooleanValue = "true";

        private static readonly Guid UniqueIdentifierValue = new Guid("{C6235168-307B-4225-9409-A26919B080C0}");
        private const string SerializedUniqueIdentifierValue = "c6235168-307b-4225-9409-a26919b080c0";

        private static readonly XmlDocument XmlValue;
        private const string SerializedXmlValue = "<?xml version=\"1.0\"?><configuration><connectionStrings><add name=\"\" connectionString=\"\" providerName=\"System.Data.SqlClient\" /></connectionStrings></configuration>";

        static ParameterValueSerializerTests()
        {
            XmlValue = new XmlDocument();
            XmlValue.LoadXml(SerializedXmlValue);
        }
        #endregion

        [Test]
        public void SerializeString()
        {
            var serializedValue = ParameterValueSerializer.Serialize(StringValue);

            Assert.AreEqual(SerializedStringValue, serializedValue);
        }

        [Test]
        public void DeserializeString()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsString(SerializedStringValue);

            Assert.AreEqual(StringValue, deserializedValue);
        }

        [Test]
        public void SerializeInteger()
        {
            var serializedValue = ParameterValueSerializer.Serialize(IntegerValue);

            Assert.AreEqual(SerializedIntegerValue, serializedValue);
        }

        [Test]
        public void DeserializeInteger()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsInteger(SerializedIntegerValue);

            Assert.AreEqual(IntegerValue, deserializedValue);
        }

        [Test]
        public void SerializeDecimal()
        {
            var serializedValue = ParameterValueSerializer.Serialize(DecimalValue);

            Assert.AreEqual(SerializedDecimalValue, serializedValue);
        }

        [Test]
        public void DeserializeDecimal()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsDecimal(SerializedDecimalValue);

            Assert.AreEqual(DecimalValue, deserializedValue);
        }

        [Test]
        public void SerializeDateTime()
        {
            var serializedValue = ParameterValueSerializer.Serialize(DateTimeValue);

            Assert.AreEqual(SerializedDateTimeValue, serializedValue);
        }

        [Test]
        public void DeserializeDateTime()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsDateTime(SerializedDateTimeValue);

            Assert.AreEqual(DateTimeValue.ToLocalTime(), deserializedValue);
        }

        [Test]
        public void SerializeBoolean()
        {
            var serializedValue = ParameterValueSerializer.Serialize(BooleanValue);

            Assert.AreEqual(SerializedBooleanValue, serializedValue);
        }

        [Test]
        public void DeserializeBoolean()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsBoolean(SerializedBooleanValue);

            Assert.AreEqual(BooleanValue, deserializedValue);
        }

        [Test]
        public void SerializeUniqueIdentifier()
        {
            var serializedValue = ParameterValueSerializer.Serialize(UniqueIdentifierValue);

            Assert.AreEqual(SerializedUniqueIdentifierValue, serializedValue);
        }

        [Test]
        public void DeserializeUniqueIdentifier()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsUniqueIdentifier(SerializedUniqueIdentifierValue);

            Assert.AreEqual(UniqueIdentifierValue, deserializedValue);
        }

        [Test]
        public void SerializeXml()
        {
            var serializedValue = ParameterValueSerializer.Serialize(XmlValue);

            Assert.AreEqual(SerializedXmlValue, serializedValue);
        }

        [Test]
        public void DeserializeXml()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsXml(SerializedXmlValue);

            Assert.AreEqual(XmlValue, deserializedValue);
        }

        [Test]
        public void SerializeNullXml()
        {
            var serializedValue = ParameterValueSerializer.Serialize((XmlDocument) null);

            Assert.AreEqual(null, serializedValue);
        }

        [Test]
        public void DeserializeNullXml()
        {
            var deserializedValue = ParameterValueSerializer.DeserializeAsXml(null);

            Assert.AreEqual(null, deserializedValue);
        }
    }
}
